package sury.web.controller.back;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sury.biz.service.GoodsOrderService;
import sury.common.result.Result;
import sury.dal.dto.GoodsOrderDTO;
import sury.dal.dto.OrderUserInfoDTO;
import sury.dal.entity.GoodsOrderEntity;
import sury.dal.vo.GoodsOrderVO;

/**
 * 后端接口-商品订单
 */
@RestController
@Slf4j
@RequestMapping("/back/goods-order")
public class GoodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;

    /**
     * 分页查询
     * @param goodsOrderDTO
     * @return
     */
    @GetMapping("/page")
    public Result pageOrder(GoodsOrderDTO goodsOrderDTO){
        Page<GoodsOrderEntity> page=goodsOrderService.pageOrder(goodsOrderDTO);
        return Result.success(page);
    }

    /**
     * 根据id查看订单详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long id){
        GoodsOrderVO goodsOrderVO=goodsOrderService.detail(id);
        return Result.success(goodsOrderVO);
    }

    /**
     * 修改收货人信息
     * TODO：详细地址
     * @param orderUserInfoDTO
     * @return
     */
    @PostMapping("/update")
    public Result updateUserInfo(@RequestBody OrderUserInfoDTO orderUserInfoDTO){
        GoodsOrderEntity goodsOrderEntity= BeanUtil.copyProperties(orderUserInfoDTO,GoodsOrderEntity.class);
        goodsOrderService.updateById(goodsOrderEntity);
        return Result.success();
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result deleteOrderById(@PathVariable("id") Long id){
        goodsOrderService.removeById(id);
        return Result.success();
    }

    //创建订单（临时）
    @PostMapping("/save")
    public Result saveOrder(@RequestBody GoodsOrderEntity goodsOrderEntity){
        return Result.success(goodsOrderService.save(goodsOrderEntity));
    }

}
